---
title: "IV - CDAW"
output: html_notebook
---

For preparing the IV data, I use the Organized Dissent Against War Dataset Project (Ortega 2022) and complement the information of violent resistance with ViPPA (Osorio et al 2021).

```{r}
library(tidyverse)
library(readstata13)
library(foreign)
library(readxl)
library(writexl)

rcs_data <- read_xlsx("Ortega(2022)-ODAW_v1.1.xlsx")

rcs_data <- rcs_data %>%
  mutate(p_strat = case_when(
    protests == 1 | demonstrations == 1 | direct_actions == 1 ~ 1, 
    protests == 0 & demonstrations == 0 & direct_actions == 0 ~ 0),
    s_strat = case_when(
    territory_off == 1 | active_terrc == 1 | soc_nc == 1 | pol_nc == 1 |
      eco_nc == 1 | soc_int == 1 | pol_int == 1 | eco_int == 1 ~ 1, 
    territory_off == 0 & active_terrc == 0 & soc_nc == 0 & pol_nc == 0 &
      eco_nc == 0 & soc_int == 0 & pol_int == 0 & eco_int == 0 ~ 0),
    v_strat = case_when(
    ua_civilians == 1 | ua_groups == 1 | terr_control == 1 | clashes == 1 ~ 1, 
    ua_civilians == 0 & ua_groups == 0 & terr_control == 0 & clashes == 0 ~ 0))

rcs_data_r <- rcs_data %>%
  dplyr::filter(t_rebels == 1)

```

This is to include ViPPA:

```{r}
vippa <- read.dta13("ViPPA_v1_2.dta")

vippa_mil <- vippa %>%
  filter(actor_main == "Paramilitaries")

vippa_mil <- vippa_mil %>%
  filter(year < 2006)

vippa_mil %>%
  group_by(actor_sub, actor) %>%
  count()

vippa_mil_my <- vippa_mil %>%
  group_by(mun, year, actor_sub) %>%
  summarise(reports = n(), na.rm = TRUE)

w_vippa_mil_my <- spread(vippa_mil_my, actor_sub, reports)

w_vippa_mil_my <- w_vippa_mil_my %>%
  rename(mil_gen = "Para. Generic",
         soc_cl = "Social cleansing",
         indep = "Independent_(310)")

w_vippa_mil_my <- w_vippa_mil_my %>%
  mutate_at(vars(mil_gen:indep)
            , ~replace_na(., 0)) 

w_vippa_mil_my <- w_vippa_mil_my %>%
  mutate(c_mil_gen = if_else(mil_gen > 1, 1, 0), # This is to maintain the selection criterion of the dataset
         d_mil_gen = if_else(mil_gen > 0, 1, 0),
         d_AUC = if_else(AUC > 0, 1, 0),
         d_ERPAC = if_else(ERPAC > 0, 1, 0),
         d_AGC = if_else(AGC > 0, 1, 0),
         d_ACC = if_else(ACC > 0, 1, 0),
         d_ACCU = if_else(ACCU > 0, 1, 0),
         d_soc_cl = if_else(soc_cl > 0, 1, 0),
         d_indep = if_else(indep > 0, 1, 0)
         )

w_vippa_mil_my <- w_vippa_mil_my %>%
  mutate(cr_v3 = case_when( # This is with the criterion!!!
    c_mil_gen == 1 & d_AUC == 0 & d_ERPAC == 0 & d_AGC == 0 
    & d_ACCU == 0 & d_ACC == 0 ~ 1,
    d_AUC == 1 | d_ERPAC == 1 | d_AGC == 1 | d_ACCU == 1 |
      d_ACC == 1 ~ 0,
    TRUE ~ 0))

w_vippa_mil_my <- w_vippa_mil_my %>%
  mutate(cr_v2 = case_when(
    d_mil_gen == 1 & d_AUC == 0 & d_ERPAC == 0 & d_AGC == 0 
    & d_ACCU == 0 & d_ACC == 0 ~ 1,
    d_AUC == 1 | d_ERPAC == 1 | d_AGC == 1 | d_ACCU == 1 |
      d_ACC == 1 ~ 0,
    TRUE ~ 0))

w_vippa_mil_my %>%
  group_by(cr_v3) %>%
  count()

str(w_vippa_mil_my)

```

Merge the two datasets:

```{r}
w_vippa_mil_my <- w_vippa_mil_my %>%
  rename(divipola = mun)

cdaw_iv <- left_join(rcs_data_r, w_vippa_mil_my)

cdaw_iv <- cdaw_iv %>%
  mutate_at(vars(mil_gen:cr_v2)
            , ~replace_na(., 0)) 

cdaw_iv <- cdaw_iv %>%
  rename(vc_param = cr_v3)

cdaw_iv <- cdaw_iv %>%
  mutate(v_strat2 = case_when(
    v_strat == 1 | vc_param == 1 ~ 1,
    v_strat == 0 & vc_param == 0 ~ 0
  ))

cdaw_iv <- cdaw_iv %>%
  dplyr::relocate(divipola:year, p_strat:v_strat, v_strat2)

cdaw_iv <- cdaw_iv %>%
  mutate(r_cum2 = v_strat2 + s_strat + p_strat,
         cr2 = if_else(r_cum2 > 0, 1, 0),
         r_cum = v_strat + s_strat + p_strat,
         cr = if_else(r_cum > 0, 1, 0))

cdaw_iv <- cdaw_iv %>%
  dplyr::relocate(divipola:year, p_strat:v_strat, v_strat2, r_cum2:cr)

cdaw_iv <- cdaw_iv %>%
  mutate(r_strat = case_when(
    v_strat == 1 ~ 3, # V
    s_strat == 1 & v_strat == 0 ~ 2, # S
    p_strat == 1 & v_strat == 0 & s_strat == 0 ~ 1, # P
    p_strat == 0 & v_strat == 0 & s_strat == 0 ~ 0, # NR
  ))

cdaw_iv <- cdaw_iv %>%
  mutate(r_strat2 = case_when(
    v_strat2 == 1 ~ 3, # V
    s_strat == 1 & v_strat2 == 0 ~ 2, # S
    p_strat == 1 & v_strat2 == 0 & s_strat == 0 ~ 1, # P
    p_strat == 0 & v_strat2 == 0 & s_strat == 0 ~ 0, # NR
  ))

cdaw_iv <- cdaw_iv %>%
  dplyr::select(divipola,year, p_strat:cr, r_strat:r_strat2, t_rebels, t_state, t_progov)

write_xlsx(cdaw_iv, "Ortega(2024)-IV_FV(09302024).xlsx")
  # Checked.

```

A brief note on the name of the files:

I will keep the original names of the raw datasets used for the analysis. Whenever I have intervened or modified a dataset (e.g., by creating a dummy out of a numeric variable), I will name the dataset as, "Ortega(2024)...".